import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.resps.Tuple;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-07-01
 * Time：15:16
 */
public class RedisDemoZset {
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis = jedisPool.getResource()) {
            test5(jedis);
        }
    }

    private static void test5(Jedis jedis) {
        System.out.println("zrank");
        jedis.flushDB();

        jedis.zadd("key", 10, "zhangsan");
        jedis.zadd("key", 20, "lisi");
        jedis.zadd("key", 30, "wangwu");

        Long rank = jedis.zrank("key", "lisi");
        System.out.println(rank);
    }

    private static void test4(Jedis jedis) {
        System.out.println("zscore");
        jedis.flushDB();

        jedis.zadd("key", 10, "zhangsan");
        jedis.zadd("key", 20, "lisi");
        jedis.zadd("key", 30, "wangwu");

        Double score = jedis.zscore("key", "zhangsan");
        System.out.println("score: " + score);
    }

    private static void test3(Jedis jedis) {
        System.out.println("zrem");
        jedis.flushDB();

        jedis.zadd("key", 10, "zhangsan");
        jedis.zadd("key", 20, "lisi");
        jedis.zadd("key", 30, "wangwu");

        jedis.zrem("key", "zhangsan");
        jedis.zrem("key", "lisi");

        List<Tuple> ret = jedis.zrangeWithScores("key", 0, -1);
        System.out.println(ret);
    }

    private static void test2(Jedis jedis) {
        System.out.println("zcard");
        jedis.flushDB();

        jedis.zadd("key", 10, "zhangsan");
        jedis.zadd("key", 20, "lisi");
        jedis.zadd("key", 30, "wangwu");

        System.out.println(jedis.zcard("key"));

    }

    private static void test1(Jedis jedis) {
        jedis.flushDB();
        System.out.println("zadd/zrange");

        jedis.zadd("key", 77.7, "zhangsan");
        Map<String, Double> map = new HashMap<>();
        map.put("lisi", 88.8);
        map.put("wangwu", 93.3);
        jedis.zadd("key", map);

        List<String> ret = jedis.zrange("key", 0, -1);
        System.out.println(ret);

        //元组：不是标准库中的，jedis提供的
        List<Tuple> ret2 = jedis.zrangeWithScores("key", 0, -1);
        System.out.println(ret2.get(0).getElement());
        System.out.println(ret2.get(0).getScore());

        System.out.println(ret2);
    }
}
